package com.gali.mall.controller;

import com.gali.mall.common.ResultInfo;
import com.gali.mall.exception.GaLiExceptionEnum;
import com.gali.mall.filiter.UserFilter;
import com.gali.mall.model.pojo.Order;
import com.gali.mall.model.vo.OrderVO;
import com.gali.mall.request.orderReq.AddOrderReq;
import com.gali.mall.request.orderReq.SelectOrderReq;
import com.gali.mall.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author gali
 * @date 2022/4/25 21:33
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param addOrderReq
     * @return
     */
    @PostMapping("/add")
    public ResultInfo addOrder(@RequestBody @Valid AddOrderReq addOrderReq){
        Order order = new Order();
        BeanUtils.copyProperties(addOrderReq,order);
        order.setUserId(UserFilter.currentUser.getId());
        String s = orderService.addOrder(order);
        return ResultInfo.success(s);
    }
//    @PostMapping("/delete")
//    public ResultInfo deleteOrder(@RequestBody @Valid DeleteOrderReq deleteOrderReq){
//        boolean b = orderService.deleteOrder(deleteOrderReq.getId());
//        if (b){
//            return ResultInfo.success();
//        }
//        return ResultInfo.error(GaLiExceptionEnum.DELETE_ERROR);
//    }
//    @PostMapping("/update")
//    public ResultInfo updateOrder(@RequestBody @Valid UpdateOrderReq updateOrderReq){
//        Order order = new Order();
//        BeanUtils.copyProperties(updateOrderReq,order);
//        Integer id = UserFilter.currentUser.getId();
//        order.setUserId(id);
//        boolean b = orderService.reviseOrder(order);
//        if (b){
//            return ResultInfo.success();
//        }
//        return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
//    }

    /**
     * 订单详情
     * @param selectOrderReq
     * @return
     */
    @PostMapping("/selectInfo")
    public ResultInfo selectInfo(@RequestBody @Valid SelectOrderReq selectOrderReq){
        OrderVO orderVO = orderService.selectByOrderNo(selectOrderReq.getOrderNo());
        if (orderVO!=null){
            return ResultInfo.success(orderVO);
        }
        return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
    }

    /**
     * 查询所有订单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/listForCustomer")
    public ResultInfo listForCustomer(@RequestParam("pageNum") Integer pageNum , @RequestParam("pageSize") Integer pageSize){
        PageInfo pageInfo = orderService.listForCustomer(pageNum, pageSize);
        return ResultInfo.success(pageInfo);
    }

    /**
     * 取消订单
     * @param orderNo
     * @return
     */
    @PostMapping("/cancel")
   public ResultInfo cancelOrder(@RequestParam("orderNo") String orderNo){
       boolean b = orderService.cancelOrder(orderNo);
       if (b){
           return ResultInfo.success();
       }
       return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
   }

    /**
     * 获取二维码
     * @param orderNo
     * @return
     */
   @PostMapping("/getCode")
   public ResultInfo codeInfo(@RequestParam("orderNo") String orderNo){
       String code = orderService.code(orderNo);
       return ResultInfo.success(code);
   }

    /**
     * 支付订单
     * @param orderNo
     * @return
     */
   @PostMapping("/pay")
   public ResultInfo payOrder (@RequestParam("orderNo") String orderNo) {
       boolean b = orderService.payOrder(orderNo);
       if (b){
           return ResultInfo.success();
       }
       return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
   }

    /**
     * 订单发货
     * @param orderNo
     * @return
     */
   @PostMapping("/admin/send")
   public ResultInfo sendOrder (@RequestParam("orderNo") String orderNo) {
       boolean b = orderService.sendOrder(orderNo);
       if (b){
           return ResultInfo.success();
       }
       return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
   }

    /**
     * 订单完结
     * @param orderNo
     * @return
     */
   @PostMapping("/finish")
   public ResultInfo finishOrder(@RequestParam("orderNo") String orderNo){
       boolean b = orderService.finishOrder(orderNo);
       if (b){
           return ResultInfo.success();
       }
       return ResultInfo.error(GaLiExceptionEnum.UPDATE_ERROR);
   }

    /**
     * 查询所有订单（后台）
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/admin/listForAdmin")
    public ResultInfo listForAdmin(@RequestParam("pageNum") Integer pageNum , @RequestParam("pageSize") Integer pageSize){
        PageInfo pageInfo = orderService.listForAdmin(pageNum, pageSize);
        return ResultInfo.success(pageInfo);
    }
}
